package com.xcc.netty.socket.nio;

import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.util.Scanner;

/**
 * @author frosty
 * @description
 * @date 2019-12-31 31:10:46
 */


public class NIOClient {

    public static void main(String[] args) throws Exception {
        //得到一个网络通道
        SocketChannel socketChannel = SocketChannel.open();
        //设置非阻塞
        socketChannel.configureBlocking(false);
        InetSocketAddress address = new InetSocketAddress("127.0.0.1", 10000);
        Scanner scanner=new Scanner(System.in);
        //连接服务器
        boolean blo = socketChannel.connect(address);
        if (!blo) {
            while (!socketChannel.finishConnect()) {
                System.out.println("因为连接需要时间  客户端不会阻塞  do something");
            }
        }

        System.out.println("连接成功");
      //  System.in.read();
        //连接成功
 while (true) {
     String s = scanner.nextLine();
     String msg = "我是" + Thread.currentThread().getName() + " id" + Thread.currentThread().getId() + s;
     //wrap  创建一个buffer
     System.out.println(msg);
     ByteBuffer buffer = ByteBuffer.wrap(msg.getBytes());
     System.out.println(buffer.array().length);
     socketChannel.write(buffer);



 }
    }
}